package com.common.util;

import java.util.HashMap;
import java.util.Map;
import java.lang.String;

public class TypeUtils {

    private static final Map<String, String> javaTypeMap = new HashMap<>();
    private static final Map<String, String> javaTypePathMap = new HashMap<>();
    private static final Map<String, String> descMap = new HashMap<>();

    public static String getJavaType(String jdbcTYpe) {
        jdbcTYpe = jdbcTYpe.toLowerCase();
        String javaType = javaTypeMap.get(jdbcTYpe);
        if (javaType != null) {
            return javaType;
        }
        switch (jdbcTYpe) {
            case "decimal":
                javaType = "BigDecimal";
                break;
            case "int":
            case "bigint":
            case "smallint":
            case "tinyint":
            case "integer":
            case "bit":
                javaType = "Integer";
                break;
            case "double precision":
                javaType = "Double";
                break;
            case "varchar":
            case "char":
            case "text":
            case "character":
                javaType = "String";
                break;
            case "date":
            case "datetime":
            case "timestamp":
                javaType = "Date";
                break;
            case "float":
                javaType = "Float";
                break;
            default:
        }
        if (javaType == null) {
            throw new NullPointerException(jdbcTYpe + " can not find javaTyp,place add");
        }
        javaTypeMap.put(javaType, javaType);

        return javaType;
    }

    public static String getJavaTypePath(String jdbcTYpe) {
        jdbcTYpe = jdbcTYpe.toLowerCase();
        String javaType = javaTypePathMap.get(jdbcTYpe);
        if (javaType != null) {
            return javaType;
        }
        switch (jdbcTYpe) {
            case "decimal":
                javaType = "java.math.BigDecimal";
                break;
            case "int":
            case "bigint":
            case "smallint":
            case "tinyint":
            case "integer":
            case "bit":
                javaType = "java.lang.Integer";
                break;
            case "double precision":
                javaType = "java.lang.Double";
                break;
            case "varchar":
            case "char":
            case "text":
            case "character":
                javaType = "java.lang.String";
                break;
            case "date":
            case "datetime":
            case "timestamp":
                javaType = " java.util.Date";
                break;
            case "float":
                javaType = "java.lang.Float";
                break;
            default:
        }
        if (javaType == null) {
            throw new NullPointerException(jdbcTYpe + " can not find javaTyp,place add");
        }
        javaTypePathMap.put(javaType, javaType);

        return javaType;
    }

    /**
     * 获取描述类型
     *
     * @param jdbcType
     * @return
     */
    public static String getDescByJdbcType(String jdbcType) {
        jdbcType = jdbcType.toLowerCase();
        String desc = descMap.get(jdbcType);
        if (desc != null) {
            return desc;
        }
        switch (jdbcType) {
            case "int":
            case "bigint":
            case "smallint":
            case "tinyint":
            case "integer":
            case "bit":
                desc = "整型";
                break;
            case "double precision":
                desc = "浮点型";
                break;
            case "varchar":
            case "char":
            case "text":
            case "character":
                desc = "字符串类型";
                break;
            case "date":
            case "datetime":
            case "timestamp":
                desc = " 时间日期类型";
                break;
            case "float":
                desc = "小数类型";
                break;
            default:
        }
        if (desc == null) {
            desc = jdbcType;
        }
        descMap.put(jdbcType, desc);
        return desc;

    }
}
